package com.cti.ftpservice.ws.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.activation.DataHandler;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.ws.BindingType;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Service;

import com.cti.ftpservice.ftp.DateHelper;
import com.cti.ftpservice.utils.Base64CoderUtils;
import com.cti.ftpservice.utils.FileHelper;
import com.cti.ftpservice.vo.FileVO;
import com.cti.ftpservice.vo.PageViewVO;
import com.cti.ftpservice.ws.BookCatalogWS;
import com.ctibook.booksystem.common.Constants;
import com.ctibook.booksystem.service.bookcatalog.BookInfoService;
import com.ctibook.booksystem.service.bookcatalog.BookInfoUploadFileRecordService;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoCategoryVos;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoDetailsVO;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoPropertyLoadVo;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoPublisherVo;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoSearchNormalVO;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoStatisticsTJVo;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoUpdateVo;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoUploadFileRecordVO;

@Service("bookCatalogWS")
@WebService(serviceName = "BookCatalogWSImpl", endpointInterface = "com.cti.ftpservice.ws.BookCatalogWS", targetNamespace = "http://impl.ws.ftpservice.cti.com/")
@SuppressWarnings({ "unchecked", "deprecation" })
public class BookCatalogWSImpl implements BookCatalogWS {

	@Resource
	private BookInfoService bookInfoService;
	
	@Resource
	private BookInfoUploadFileRecordService bookInfoUploadFileRecordService;

	 
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSSSS");
	@Override
	public void uploadBookInfoFile2(String userId,String userName, FileVO fileVo,Boolean isCheckedIsbn) throws WSException {
		String dirPath = FileHelper.createDirWithBaseClassPath(Constants.BOOKINFO_UPLOAD_DIR_2+"_"+DateHelper.getYearFromDate(new Date()));
		Date date = new Date();
		String dateFormat = sdf.format(date);
		String fileName = dateFormat + fileVo.getFileType();
		String tempFilePath = FilenameUtils.concat(dirPath, fileName + ".bak");

		DataHandler handler = fileVo.getFileContent();
		try {
			InputStream is = handler.getInputStream();

			OutputStream os = new FileOutputStream(tempFilePath);
			byte[] b = new byte[1024];
			int bytesRead = 0;
			while ((bytesRead = is.read(b)) != -1) {
				os.write(b, 0, bytesRead);
			}
			os.flush();
			os.close();
			is.close();

			String filePath = FilenameUtils.concat(dirPath, fileName);
			File targetFile = new File(filePath);
			if (targetFile.exists())
				targetFile.delete();
			File sourceFile = new File(tempFilePath);
			sourceFile.renameTo(targetFile);

			bookInfoUploadFileRecordService.addBookInfoUploadFileRecord(
					FilenameUtils.concat(Constants.BOOKINFO_UPLOAD_DIR_2+"_"+DateHelper.getYearFromDate(new Date()), fileName), userId,userName,isCheckedIsbn);
		} catch (Exception ex) {
			throw new WSException(ex.getMessage());
		}

	}

	public PageViewVO<BookInfoDetailsVO> fullTextSearchBookInfoVisiblePropertyByNormalSearchVO(BookInfoSearchNormalVO vo, int pageIndex, int pageSize,BookInfoPropertyLoadVo bookInfoPropertyLoadVo) throws WSException {
		try {
			PageViewVO<BookInfoDetailsVO> pg = bookInfoService.fullTextSearchByNormalSearchVO(vo, pageIndex, pageSize,bookInfoPropertyLoadVo);
			pg = (PageViewVO<BookInfoDetailsVO>) Base64CoderUtils.encoderObj2(pg);
			return pg;
		} catch (Exception e) {
			throw new WSException(e.getMessage());
		}
	}

	@Override
	public BookInfoDetailsVO fullTextSearchFetchUniqueBookByIsbn(String isbn) throws WSException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BookInfoCategoryVos> getAllBookInfoCategoryVos() {
		
		return bookInfoService.getAllBookInfoCategoryVos();
	}

	@Override
	public List<BookInfoPublisherVo> getAllBookInfoPublisherVos() {
		return bookInfoService.getAllBookInfoPublisherVos();
	}
	
	public List<BookInfoUploadFileRecordVO> getBookInfoUploadFileRecordVO(String startDate, String endDate, String parsered, String operaterId) {
		return bookInfoUploadFileRecordService.getBookInfoUploadFileRecordVO(startDate,endDate,parsered,operaterId);
	}
	
	public void updateBookInfoIsBlackBookByIsbns(String[] isbns,boolean isBlackBook){
		bookInfoService.updateBookInfoIsBlackBookByIsbns(isbns, isBlackBook);
	}
	
	public String updateBookInfoPriceAndCurrency(BookInfoUpdateVo[] updateVos){
		return bookInfoService.updateBookInfoPriceAndCurrency(updateVos);
	}
	
	public void deleteBookInfoByIsbns(String[] isbns){
		bookInfoService.deleteBookInfoByIsbns(isbns);
	}
	
	public  List<BookInfoStatisticsTJVo> getBookInfoStatisticsVosByBookInfoSearchNormalVO(BookInfoSearchNormalVO bookInfoSearchNormalVO) {
		return bookInfoService.getBookInfoStatisticsVosByBookInfoSearchNormalVO(bookInfoSearchNormalVO);
	}
}
